package com.xing.controller;

import com.alibaba.fastjson.JSON;
import com.xing.entity.pojo.RoleFunctionKey;
import com.xing.entity.pojo.SystemFunction;
import com.xing.entity.vo.AuthorityUser;
import com.xing.service.RoleFunctionService;
import com.xing.service.SystemFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.PublicKey;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/function")
public class FunctionController {

    @Autowired
    private SystemFunctionService systemFunctionService;

    @Autowired
    private RoleFunctionService roleFunctionService;


    @RequestMapping("/findFunAll")
    public String findFunAll(Model model){
        List<AuthorityUser> funList = systemFunctionService.findFunAll();
        model.addAttribute("funList",funList);
        return "function/function";
    }

    @RequestMapping("/findByFun")
    @ResponseBody
    public String findByFun(@RequestParam("funId") String funId) {
        return JSON.toJSONString(systemFunctionService.selectByPrimaryKey(funId));
    }
    @RequestMapping("/update")
    public String update(SystemFunction systemFunction)
    {
        systemFunctionService.updateByPrimaryKeySelective(systemFunction);
        return "redirect:/function/findFunAll";
    }

    @RequestMapping("/addFatherFun")
    public String addFatherFun(SystemFunction systemFunction)
    {
        systemFunction.setFunId(UUID.randomUUID().toString());
        systemFunction.setFunPid("-1");
        systemFunctionService.insertSelective(systemFunction);
        return "redirect:/function/findFunAll";
    }

    @RequestMapping("/deleteSelectedFun")
    @ResponseBody
    public String deleteSelectedFun(String[] ids){
        String msg;
        Boolean aBoolean = systemFunctionService.deletePrFunction(ids);
        if(aBoolean){
            msg="删除成功！";
        }else {
            msg="删除失败！";
        }
        return msg;
    }

    @RequestMapping("/deleteFatherFun")
    @ResponseBody
    public String deleteFatherFun(String funId){
        String msg;
        int i = systemFunctionService.deleteByPrimaryKey(funId);
        if(i>0){
            msg="删除成功！";
        }else {
            msg="删除失败！";
        }
        return msg;
    }

    @RequestMapping("/addChFun")
    public String addChFun(SystemFunction systemFunction){
        systemFunction.setFunId(UUID.randomUUID().toString());
        systemFunctionService.insertSelective(systemFunction);
        return "redirect:/function/findFunAll";
    }

    @RequestMapping("/findByRoleFunId")
    @ResponseBody
    public Boolean findByRoleFunId(String funId){
        List<RoleFunctionKey> list = roleFunctionService.findByFunId(funId);
        return list.size() <= 0;
    }

}
